<?php


namespace app\admin\controller\question;


use app\common\controller\Adminbase;
use think\Db;

class Question extends Adminbase
{
    protected function initialize()
    {
        $question_cate_list = Db::name('question_cate')
            ->where([
                'is_del' => 0,
            ])
            ->order('sort desc')
            ->select();
        foreach ($question_cate_list as $k=>$v){
            $qc[$v['id']]=$v['name'];
        }
        $this->assign('qc',$qc);
        $this->assign('question_cate_list',$question_cate_list);
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit,$where2) = $this->buildTableParames();
            $where[]=['is_del','=',0];
            $_list = Db::name('question')
                ->page($page, $limit)
                ->where($where)
                ->where($where2)
                ->order('sort')
                ->select();
            foreach ($_list as $k=>$v){
                $qcate=Db::name('question_cate')->find($v['question_cate_id']);
                $_list[$k]['question_cate_id']=$qcate['name'];
            }
            $total = Db::name('question') ->where($where)->where($where2)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }
    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('question')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            //软删除
            foreach ($ids as $id) {
                Db::name('question')->where('id', $id)->update([
                    'is_del'=>1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $insert_data = array(
                'question_cate_id' => $data['question_cate_id'],
                'question' => $data['question'],
                'answer' => $data['answer'],
                'sort' => $data['sort'],
            );

            $stmt = Db::name('question')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {

            return $this->fetch();
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['id'],
                'question_cate_id' => $data['question_cate_id'],
                'question' => $data['question'],
                'answer' => $data['answer'],
                'sort' => $data['sort'],
            );
            $stmt = Db::name('question')->update($update_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('question')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }


}